from re import A, T
from django.db import models
from user.models import User
from django.utils import timezone

# Create your models here.
class ArticleCategory(models.Model):
    '''文章分类'''
    # 文章标题
    title = models.CharField(max_length=255,blank=True,verbose_name='标题')
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title
    
    class Meta:
        db_table = 'bl_category' #修改表名
        verbose_name = '分类管理' # admin站点显示
        verbose_name_plural = verbose_name

class Article(models.Model):
    '''文章'''
    # 作者
    author = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='作者')
    # 标题图
    avatar = models.ImageField(upload_to='article/%Y%m%d',blank=True,verbose_name='标题图')
    # 标题
    title = models.CharField(max_length=20,blank=True)
    # 分类
    category = models.ForeignKey(ArticleCategory, on_delete=models.CASCADE,blank=True,null=True,related_name='article',verbose_name='分类')
    # 标签
    tags = models.CharField(max_length=20,blank=True,verbose_name='标签')
    # 摘要信息
    summary = models.CharField(max_length=200,blank=False,null=True,verbose_name='摘要信息')
    # 文章正文
    content = models.TextField(verbose_name='文章正文')
    # 浏览量
    total_views = models.PositiveIntegerField(default=0,verbose_name='浏览量')
    # 评论量
    comment_num = models.PositiveIntegerField(default=0,verbose_name='评论量')
    # 文章创建时间
    create_time = models.DateTimeField(verbose_name='文章创建时间',default=timezone.now)
    # 文章修改时间
    update_time = models.DateTimeField(auto_now=True,verbose_name='文章修改时间')

    def __str__(self):
        return self.title

    class Meta:
        db_table = 'bl_artcle'
        ordering = ('-create_time',)
        verbose_name = '文章管理'
        verbose_name_plural = verbose_name

class Comment(models.Model):
    # 评论内容
    content = models.TextField()
    # 评论文章
    article = models.ForeignKey(Article,on_delete='SET_NULL',null=True)
    # 评论用户
    user = models.ForeignKey(User, on_delete='SET_NULL',null=True)
    # 评论时间
    comment_time = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.article.title

    class Meta:
        db_table = 'bl_comment'
        verbose_name = '评论管理'
        verbose_name_plural = verbose_name


